#include "pwmcontroller.h"


PWMController::PWMController(QObject *parent) : QObject(parent)
{
    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &PWMController::updatePWM);
    dutyCycle = 50; // 初始占空比为 50%
    period = 100; // 周期为 100 毫秒
    timer->start(1); // 每 1 毫秒检查一次
}

void PWMController::setDutyCycle(int newDutyCycle)
{
    if (newDutyCycle >= 0 && newDutyCycle <= 100)
    {
        dutyCycle = newDutyCycle;
    }
}
extern "C"{
extern void led_onOff(int isOn);
}
void PWMController::updatePWM()
{
    static int counter = 0;
    counter++;
    if (counter > period)
    {
        counter = 0;
    }
    if (counter <= dutyCycle)
    {
        led_onOff(1);
    }
    else
    {
        led_onOff(0);
    }
}
